package com.example.leetcode.geometry;

import lombok.extern.slf4j.Slf4j;

@Slf4j
class SolutionLCP74 {
    public int fieldOfGreatestBlessing(int[][] forceField) {
        int n = forceField.length;
        int max = 1;
        for (int i = 0; i < n; i++) {
            int count = 1;
            double[] rec1 = getRec(forceField[i]);
            for (int j = 0; j < n; j++) {
                if (i == j) {
                    continue;
                }
                double[] rec2 = getRec(forceField[j]);
                if (isRectangleOverlap(rec1, rec2)) {
                    count++;
                }
            }
            if (count > max) {
                max = count;
            }
        }
        return max;
    }

    private static double[] getRec(int[] forceField) {
        int centerX = forceField[0];
        int centerY = forceField[1];
        double len = forceField[2] * 1.0 / 2;
        return new double[]{centerX - len, centerY - len, centerX + len, centerY + len};
    }

    public boolean isRectangleOverlap(double[] rec1, double[] rec2) {
        return (Math.min(rec1[2], rec2[2]) >= Math.max(rec1[0], rec2[0]) &&
                Math.min(rec1[3], rec2[3]) >= Math.max(rec1[1], rec2[1]));
    }

}

/**
 * 解答错误
 */
public class LCP74 {
    public static void main(String[] args) {
        SolutionLCP74 solution3 = new SolutionLCP74();
        int[][] forceField = {{4, 4, 6}, {7, 5, 3}, {1, 6, 2}, {5, 6, 3}};
        int[][] forceField1 = {{0, 0, 1}, {1, 0, 1}};
        int[][] forceField2 = {
                {932, 566, 342},
                {546, 489, 250},
                {723, 454, 748},
                {830, 887, 334},
                {617, 534, 721},
                {924, 267, 892},
                {151, 64, 65},
                {318, 825, 196},
                {102, 941, 940},
                {748, 562, 582},
                {76, 938, 228},
                {921, 15, 245},
                {871, 96, 823},
                {701, 737, 991},
                {339, 861, 146},
                {484, 409, 823},
                {574, 728, 557},
                {104, 845, 459},
                {363, 804, 94},
                {445, 685, 83},
                {324, 641, 328},
                {626, 2, 897},
                {656, 627, 521},
                {935, 506, 956},
                {210, 848, 502},
                {990, 889, 112}
        };
        System.out.println(solution3.fieldOfGreatestBlessing(forceField)); // 3
//        System.out.println(solution3.fieldOfGreatestBlessing(forceField1)); // 2
//        System.out.println(solution3.fieldOfGreatestBlessing(forceField2)); // 13
    }
}
